package com.keyue.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.keyue.isolate.enums.DataPermissionFieldRangeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DataPermissionItemVO implements Serializable {

    /**
     * 来源平台,多选，ALL 全部
     */
    private List<DataPermissionFieldItemVO> sourcePlatformList;

    /**
     * 出发城市
     */
    private List<DataPermissionFieldItemVO> fromCityCodeList;

    /**
     * 仓库编码,多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> warehouseCodeList;

    /**
     * 承运商公司编码,多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> companyCodeList;

    /**
     * 承运商编码,多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> carrierCodeList;

    /**
     * 承运商类型编码,多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> transportTypeCodeList;

    /**
     * 承运商子类型编码,多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> subTransportTypeCodeList;

    /**
     * 下单客户,多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> customerList;

    /**
     * 合同主体，多选;ALL 全部
     */
    private List<DataPermissionFieldItemVO> contractBodyList;


    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getCarrierCodes() {
        if (CollectionUtils.isEmpty(carrierCodeList)) {
            return Collections.emptyList();
        }

        return carrierCodeList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getCompanyCodes() {
        if (CollectionUtils.isEmpty(companyCodeList)) {
            return Collections.emptyList();
        }

        return companyCodeList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getCustomerCodes() {
        if (CollectionUtils.isEmpty(customerList)) {
            return Collections.emptyList();
        }

        return customerList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getSourcePlatformCodes() {
        if (CollectionUtils.isEmpty(sourcePlatformList)) {
            return Collections.emptyList();
        }

        return sourcePlatformList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getWarehouseCodes() {
        if (CollectionUtils.isEmpty(warehouseCodeList)) {
            return Collections.emptyList();
        }

        return warehouseCodeList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getContractBodyCodes() {
        if (CollectionUtils.isEmpty(contractBodyList)) {
            return Collections.emptyList();
        }

        return contractBodyList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getTransportTypeCodes() {
        if (CollectionUtils.isEmpty(transportTypeCodeList)) {
            return Collections.emptyList();
        }

        return transportTypeCodeList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getSubTransportTypeCodes() {
        if (CollectionUtils.isEmpty(subTransportTypeCodeList)) {
            return Collections.emptyList();
        }

        return subTransportTypeCodeList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public List<String> getFromCityCodes() {
        if (CollectionUtils.isEmpty(fromCityCodeList)) {
            return Collections.emptyList();
        }

        return fromCityCodeList.stream().map(DataPermissionFieldItemVO::getCode).collect(Collectors.toList());
    }

    public static DataPermissionItemVO buildAllPermVO() {
        return new DataAllPermissionItemVO();
    }

    /**
     * 移除不需要参与数据隔离的字段
     *
     * @param
     */
    public void removeNoNeedFieldValue(List<String> modelTypes) {
        if (DataPermissionFieldRangeEnum.CONTRACT_BODY.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.contractBodyList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.CUSTOMER.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.customerList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.SOURCE_PLATFORM.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.sourcePlatformList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.WAREHOUSE_CODE.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.warehouseCodeList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.COMPANY_CODE.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.companyCodeList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.CARRIER_CODE.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.carrierCodeList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.TRANSPORT_TYPE_CODE.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.transportTypeCodeList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.SUB_TRANSPORT_TYPE_CODE.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.subTransportTypeCodeList = Collections.emptyList();
        }

        if (DataPermissionFieldRangeEnum.FROM_CITY_CODE.getModelTypeRanges().stream().noneMatch(modelTypes::contains)) {
            this.fromCityCodeList = Collections.emptyList();
        }
    }
}
